package com.example.lifeordie;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    private GridLayout windows;
    private Button news;
    private Button start1;
    private int r = 8, c = 7;
    private Button jb;
    private Button[][] jl = new Button[r][c];
    //int x=windows.getWidth()/c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        news=(Button)findViewById(R.id.news);
        start1=(Button)findViewById(R.id.start1);
        windows = (GridLayout) findViewById(R.id.windows);
        windows.setRowCount(r);
        windows.setColumnCount(c);
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++) {
                jl[i][j] = new Button(this);
                jl[i][j].setText("0");
                jl[i][j].setBackgroundColor(Color.rgb(119,125,110));//灰色代表未点击的
                Listener1(i, j);
                RelativeLayout.LayoutParams bt = new RelativeLayout.LayoutParams(110, 100);
                windows.addView(jl[i][j],bt);
            }

        news.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < r; i++)
                            for (int j = 0; j < c; j++) {
                                jl[i][j].setText("0");
                                jl[i][j].setBackgroundColor(Color.rgb(119,125,110));
                            }
                    }
                }
        );
        start1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Random(r,c);
                    }
                }
        );
    }
    public void Random(int r,int c){//随机数列方法
        int iRnd=(int)(Math.random()*r);
        int jRnd=(int)(Math.random()*c);
        if(iRnd-1>=0&&jRnd-1>=0)
            jl[iRnd-1][jRnd-1].setText("0");
        if(iRnd-1>=0)
            jl[iRnd-1][jRnd].setText("1");
        //jl[iRnd-1][jRnd].setBackgroundColor(Color.rgb(180,12,0));
        if(iRnd-1>=0&&jRnd+1<c)
            jl[iRnd-1][jRnd+1].setText("0");
        if(jRnd-1>=0)
            jl[iRnd][jRnd-1].setText("1");
        //jl[iRnd][jRnd-1].setBackgroundColor(Color.rgb(180,12,0));

        jl[iRnd][jRnd].setText("0");//中间值
        if(jRnd+1<c)
            jl[iRnd][jRnd+1].setText("0");
        if(iRnd+1<r&&jRnd-1>=0)
            jl[iRnd+1][jRnd-1].setText("0");
        if(iRnd+1<r)
            jl[iRnd+1][jRnd].setText("1");
        //jl[iRnd+1][jRnd].setBackgroundColor(Color.rgb(180,12,0));
        if(iRnd+1<r&&jRnd+1<c)
            jl[iRnd+1][jRnd+1].setText("0");
    }

    public void Listener1(final int i, final int j) {//判断自身状态的方法
        jl[i][j].setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0;
                        {
                            String str = (jl[i][j].getText().toString());
                            if ((i - 1) >= 0 && (j - 1) >= 0)
                                a2 = Integer.parseInt(jl[i - 1][j - 1].getText().toString());
                            if ((i - 1) >= 0)
                                a3 = Integer.parseInt(jl[i - 1][j].getText().toString());
                            if ((i - 1) >= 0 && (j + 1) < c)
                                a4 = Integer.parseInt(jl[i - 1][j + 1].getText().toString());
                            if ((j - 1) >= 0)
                                a5 = Integer.parseInt(jl[i][j - 1].getText().toString());
                            if ((j + 1) < c)
                                a6 = Integer.parseInt(jl[i][j + 1].getText().toString());
                            if ((i + 1) < r && (j - 1) >= 0)
                                a7 = Integer.parseInt(jl[i + 1][j - 1].getText().toString());
                            if ((i + 1) < r)
                                a8 = Integer.parseInt(jl[i + 1][j].getText().toString());
                            if ((i + 1) < r && (j + 1) < c)
                                a9 = Integer.parseInt(jl[i + 1][j + 1].getText().toString());
                            int x = a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
                            if (x == 3) {
                                jl[i][j].setText("1");
                                jl[i][j].setBackgroundColor(Color.rgb(180,12,0));//红色代表
                            } else if (x == 2) {
                                jl[i][j].setText(str);
                                jl[i][j].setBackgroundColor(Color.rgb(0,180,0));//绿色代表状态不改变
                            } else {
                                jl[i][j].setText("0");
                                jl[i][j].setBackgroundColor(Color.rgb(111,111,111));//白色代表死亡
                            }
                        }
                    }

                });
    }
}

